//    Copyright (C) Kherty.  All rights reserved.
using OpenLS.Core.Serialization;
using System.ComponentModel;

namespace OpenLS.Drawing
{
    public class Connector : IAnchored, INotifyPropertyChanged, IShape
    {
        public double? Width
        {
            get
            {
                if (ShapeProperties.Xfrm == null)
                    return null;
                return ShapeProperties.SafeXfrm.Size.Width;
            }
            set
            {
                if (value == null)
                    return;
                ShapeProperties.SafeXfrm.Size.Width = value.Value;
                NotifyPropertyChanged("Width");
            }
        }

        public double? Height
        {
            get
            {
                if (ShapeProperties.Xfrm == null)
                    return null;
                return ShapeProperties.SafeXfrm.Size.Height;
            }
            set
            {
                if (value == null)
                    return;
                ShapeProperties.SafeXfrm.Size.Height = value.Value;
                NotifyPropertyChanged("Height");
            }
        }

        private bool fPublished;
        private string macro;
        private ConnectorNonVisual nvCxnSpPr;

        private ShapeProperties spPr;

        private ShapeStyle style;

        public ConnectorNonVisual ConnectorNonVisual
        {
            get { return nvCxnSpPr; }
            set { nvCxnSpPr = value; NotifyPropertyChanged("ConnectorNonVisual"); }
        }

        public ShapeProperties ShapeProperties
        {
            get { return spPr; }
            set { spPr = value; NotifyPropertyChanged("ShapeProperties"); }
        }

        public ShapeStyle Style
        {
            get { return style; }
            set { style = value; NotifyPropertyChanged("Style"); }
        }

        public string Macro
        {
            get { return macro; }
            set { macro = value; NotifyPropertyChanged("Macro"); }
        }

        public bool PublishedToServer
        {
            get { return fPublished; }
            set { fPublished = value; NotifyPropertyChanged("PublishedToServer");}
        }

        #region IAnchored Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.Read("cxnSp"))
            {
                Macro = c.GetOptionalString("macro");
                PublishedToServer = c.GetOptionalBoolean("fPublished", false);
                ConnectorNonVisual = c.ReadElement<ConnectorNonVisual>();
                ShapeProperties = c.ReadElement<ShapeProperties>();
                Style = c.ReadOptionalElement<ShapeStyle>("style");
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("cxnSp"))
            {
                c.SetOptionalString("macro", Macro);
                c.SetOptionalBoolean("fPublished", PublishedToServer, false);
                c.WriteElement(ConnectorNonVisual);
                c.WriteElement(ShapeProperties);
                c.WriteOptionalElement(Style);
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        void  NotifyPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}